package com.zzh.takeoutapp.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.config.Constants;
import com.zzh.takeoutapp.config.UrlConfig;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.SPUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/24.
 */

public class DiscountsView extends LinearLayout implements View.OnClickListener {

    @Bind(R.id.tv_home_discounts)
    TextView tvHomeDiscounts;//满减优惠
    @Bind(R.id.tv_home_gratis)
    TextView tvHomeGratis;//免费送货
    @Bind(R.id.tv_home_merchant)
    TextView tvHomeMerchant;//新商家
    private final String HOMES_FRAGMENT = "HomesFragment";
    private Context mContext;


    public DiscountsView(Context context) {
        super(context);
        this.mContext = context;
        init(context);
    }

    public DiscountsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(context);
    }

    public DiscountsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init(context);
    }

    public TextView getTvHomeDiscounts() {
        return tvHomeDiscounts;
    }

    public void setTvHomeDiscounts(TextView tvHomeDiscounts) {
        this.tvHomeDiscounts = tvHomeDiscounts;
    }

    public TextView getTvHomeGratis() {
        return tvHomeGratis;
    }

    public void setTvHomeGratis(TextView tvHomeGratis) {
        this.tvHomeGratis = tvHomeGratis;
    }

    public TextView getTvHomeMerchant() {
        return tvHomeMerchant;
    }

    public void setTvHomeMerchant(TextView tvHomeMerchant) {
        this.tvHomeMerchant = tvHomeMerchant;
    }

    private void init(Context context) {
        this.mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.textl_ayout, this);
        ButterKnife.bind(this, view);
        initView();
        initListener();
    }

    private void initView() {
    }

    private void initListener() {
    }

    private OnDiscountClickListener onDiscountClickListener;

    public void setOnDiscountClickListener(OnDiscountClickListener onDiscountClickListener) {
        this.onDiscountClickListener = onDiscountClickListener;
    }

    public interface OnDiscountClickListener {
        void onDiscountClick(int positio);
    }

    private void initSeevice(String youhui, String feature, String pei, String str) {
        ServiceUrl.FiltrateServiceUrl(mContext,
                SPUtils.getString(mContext, Constants.ALL_LATITUDE, ""),
                SPUtils.getString(mContext, Constants.ALL_LONGITUDE, ""),
                youhui,
                feature,
                pei,
                "",
                str,
                1,
                HOMES_FRAGMENT);
    }

    @OnClick({R.id.tv_home_discounts, R.id.tv_home_gratis, R.id.tv_home_merchant})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_home_discounts:
                tvHomeDiscounts.setBackgroundResource(R.color.yellow);
                tvHomeGratis.setBackgroundResource(R.color.font_black_6);
                tvHomeMerchant.setBackgroundResource(R.color.font_black_6);
                initSeevice("mj", "", "", "");
                if (onDiscountClickListener != null) {
                    onDiscountClickListener.onDiscountClick(0);
                }
                break;
            case R.id.tv_home_gratis:
                tvHomeGratis.setBackgroundResource(R.color.yellow);
                tvHomeDiscounts.setBackgroundResource(R.color.font_black_6);
                tvHomeMerchant.setBackgroundResource(R.color.font_black_6);
                initSeevice("", "", "mian", "");
                if (onDiscountClickListener != null) {
                    onDiscountClickListener.onDiscountClick(1);
                }
                break;
            case R.id.tv_home_merchant:
                tvHomeMerchant.setBackgroundResource(R.color.yellow);
                tvHomeDiscounts.setBackgroundResource(R.color.font_black_6);
                tvHomeGratis.setBackgroundResource(R.color.font_black_6);
                initSeevice("", "", "0", "");
                if (onDiscountClickListener != null) {
                    onDiscountClickListener.onDiscountClick(2);
                }
                break;
        }
    }
}
